<template>
    <div>
        <div class='container' v-bind:class="{iscart}">
            <!-- 上部分  -->
            <div class='section section-good'>
                <checkbox-group @change="checkboxChange">
                    <div class='good' v-for='(good,index) in goodList' :key="index">

                        <!--第一个盒子  -->

                        <!-- 第二个盒子 -->
                        <div class='good_content'>
                            <div class='good_title'>
                                <!-- <div class='shangjia'></div> -->
                                <div class='shanchu'>
                                    <div class='shanchu_trash' @click="deleteList(index)">
                                        <img src='/static/images/tabBar2.png' />
                                    </div>
                                    <!-- 删除 -->
                                </div>
                            </div>
                            <div class='good_checkbox'>
                                <label class="checkbox">
                                    <checkbox style="display:none;" :value="good.isbn" :checked="good.checked" />
                                    <icon type="circle" size="23" v-if="!good.checked"></icon>
                                    <icon type="success" size="23" v-if="good.checked"></icon>
                                </label>
                            </div>
                            <div class='good_img'>
                                <img class='cover' v-bind:src='good.cover' />
                            </div>
                            <div class='content'>
                                <div class='titles'>{{good.desc}}</div>
                                <div class='titles2'>{{good.desc2}}</div>
                            </div>
                            <div class='good_footer'>
                                <div class='price'>
                                    <span class='price2'>{{good.price}}</span>
                                </div>
                                <div class='stepper'>
                                    <div :class='good.count == 1 ? "disabled": ""' class='subtract ' data-index='index' @click.stop='subtracttap(index)'>-</div>
                                    <input class='count' type='number' :value='good.count' />
                                    <div class='add' data-index="index" @click.stop='addtap(index)'>+</div>
                                </div>
                            </div>
                        </div>

                    </div>
                </checkbox-group>
            </div>

            <!-- 底部  -->
            <div class='section-bottom'>
                <checkbox-group @change="selectalltap">
                    <label class='checkbox-allcheck'>
                        <checkbox style="display:none;" :value="!checkAll" :checked="checkAll" v-bind:class='hidden' />
                        <icon type="circle" size="23" v-if="!checkAll"></icon>
                        <icon type="success" size="23" v-if="checkAll"></icon>
                        <span class='check-all-text'>全选</span>
                    </label>
                </checkbox-group>
                <div class="total">
                    <!-- <div class='totalCount'>已选择
        <span>{{totalCount}}</span>件商品
      </div> -->
                    <div class='totalPrice'>总价：
                        <span>¥{{totalPrice}}</span>
                    </div>
                </div>
                <div>
                    <div @click='settle' :class='totalCount > 0 ? "btn-primary" : "btn-default"' class='btn '>结算({{totalCount}})</div>
                </div>
            </div>

        </div>

        <!--如果购物车内没有数据，就显示没有数据-->
        <div class="cartList" v-if="iscart">

            <div>购物车什么都没有，赶快去购物吧~٩(๑❛ᴗ❛๑)۶~</div>
        </div>
    </div>
</template>


<script>
import { formatTime } from "@/utils/index";
import card from "@/components/card";
import { log } from "util";

export default {
    components: {
        card
    },

    data() {
        return {
            iscart: false, //控制购物车有没有数据
            goodList: [
                {
                    cover: "/static/images/index_pet8.png",
                    isbn: "9787535482051",
                    desc: "BROYAL CATFOOD",
                    desc2: "ADUIT & PUPPY CATS",
                    price: 139,
                    count: 1,
                    checked: false
                },
                {
                    cover: "/static/images/index_pet7.png",
                    isbn: "9787540455958",
                    desc: "BROYAL CATFOOD",
                    desc2: "ADUIT & PUPPY CATS",
                    price: 258,
                    count: 1,
                    checked: false
                }
            ],
            checkAll: false,
            totalCount: 0,
            totalPrice: 0
        };
    },

    created() {
        const logs = wx.getStorageSync("logs") || [];
        this.logs = logs.map(log => formatTime(new Date(log)));
    },
    methods: {
        /**
         * 删除商品
         */
        deleteList(index) {
            var that = this;
            var goodList = this.goodList;
            goodList.splice(index, 1);
            if (!goodList.length) {
                that.iscart = true;
                that.checkAll = false;
                that.totalCount = 0;
                that.totalPrice = 0;
            } else {
                this.calculateTotal();
            }
        },

        /**
         * 计算商品总数
         */
        calculateTotal: function() {
            var that = this;
            var goodList = this.goodList;
            // console.log(goodList);

            var totalCount = 0;
            var totalPrice = 0;
            for (var i = 0; i < goodList.length; i++) {
                var good = goodList[i];
                if (good.checked) {
                    totalCount += good.count;
                    totalPrice += good.count * good.price;
                }
            }
            totalPrice = totalPrice.toFixed(2);
            that.totalCount = totalCount;
            that.totalPrice = totalPrice;
        },

        /**
         * 用户点击商品减1
         */
        subtracttap: function(index) {
            // var index = e.target.dataset.index;
            var that = this;
            var goodList = this.goodList;
            var count = goodList[index].count;
            if (count <= 1) {
                return;
            } else {
                goodList[index].count--;

                that.goodList = goodList;

                this.calculateTotal();
            }
        },

        /**
         * 用户点击商品加1
         */
        addtap(index) {
            // var index = e.currentTarget.dataset.index;
            var that = this;
            var goodList = this.goodList;
            var count = goodList[index].count;
            goodList[index].count++;

            that.goodList = goodList;

            this.calculateTotal();
        },
        /**
         * 用户选择购物车商品
         */
        checkboxChange: function(e) {
            var that = this;
            console.log(
                "checkbox发生change事件，携带value值为：",
                e.mp.detail.value
            );
            var checkboxItems = this.goodList;
            var values = e.mp.detail.value;
            for (var i = 0; i < checkboxItems.length; ++i) {
                checkboxItems[i].checked = false;
                for (var j = 0; j < values.length; ++j) {
                    if (checkboxItems[i].isbn == values[j]) {
                        checkboxItems[i].checked = true;
                        break;
                    }
                }
            }

            var checkAll = false;
            if (checkboxItems.length == values.length) {
                checkAll = true;
            }

            that.goodList = checkboxItems;
            that.checkAll = checkAll;

            this.calculateTotal();
        },

        /**
         * 用户点击全选
         */
        selectalltap: function(e) {
            // console.log('用户点击全选，携带value值为：', e.detail.value);
            var that = this;
            var value = e.mp.detail.value;
            var checkAll = false;
            if (value && value[0]) {
                checkAll = true;
            }

            var goodList = this.goodList;
            for (var i = 0; i < goodList.length; i++) {
                var good = goodList[i];
                good["checked"] = checkAll;
            }

            that.checkAll = checkAll;
            that.goodList = goodList;

            this.calculateTotal();
        },
        settle: function() {
            wx.navigateTo({
                url: "/pages/settlement/main"
            });
        }
    }
};
</script>

<style>
page {
    height: 100%;
    background: #fff;
    font-family: -apple-system-font, Arial, Helvetica, sans-serif;
    color: #333;
    font-size: 32rpx;
    line-height: 1.42857;
}

.container {
    width: 100%;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
}

.section {
    display: flex;
    flex-direction: column;
    padding-left: 30rpx;
    padding-right: 30rpx;
    margin-bottom: 30rpx;
}

.section-good {
    margin-bottom: 100rpx;
}

.good {
    float: left;
    width: 100%;
    height: 200rpx;
    margin-top: 20rpx;
    /* padding-bottom: 20rpx; */ /* border-bottom: 1px solid #ccc; */
    background-color: #fff;
    border-bottom: 10px solid #fafafa;
}

/*  第一个盒子  */

.good .good_title {
    float: left;
    box-sizing: border-box;
    padding: 0;
    /* width: 100%; */
    height: 70rpx;
    line-height: 70rpx;
    /* border-bottom: 1px solid #ccc; */
    position: absolute;
    right: 40rpx;
}

.good .good_title view:first-of-type {
    float: left;
}

.good .good_title view:last-of-type {
    float: right;
}

.good .good_title .shanchu .shanchu_trash {
    float: left;
    margin-top: 8rpx;
    width: 56rpx;
    height: 56rpx;
}

.good .good_title .shanchu .shanchu_trash image {
    width: 100%;
    height: 100%;
}

/* 第二个盒子  */

.good .good_content {
    box-sizing: border-box;
    float: left;
    width: 100%;
    height: 188rpx;
    background-color: #fff;
    /* border-bottom: 10px solid #fafafa; */
}

.good_content .good:last-child {
    border-bottom: none;
}

.good_content .good_checkbox {
    float: left;
    width: 70rpx;
    height: 200rpx;
    line-height: 200rpx;
}

.good_content .checkbox {
    float: left;
    width: 48rpx;
    height: 48rpx;
}

.good_content .good_img {
    float: left;
    margin-top: 16rpx;
    width: 150rpx;
    height: 150rpx;
}

.good_content .good_img image {
    width: 100%;
    height: 100%;
}

.good_content .content {
    box-sizing: border-box;
    float: left;
    margin-top: 16rpx;
    margin-left: 20rpx;
    width: 384rpx;
    height: 90rpx;
}

.good_content .content .titles {
    float: left;
    text-overflow: ellipsis;
    overflow: hidden;
    font-size: 28rpx;
    /* width: 254rpx; */
    height: 42rpx;
    color: #505050;
    line-height: 42rpx;
}

.good_content .content .titles2 {
    float: left;
    text-overflow: ellipsis;
    overflow: hidden;
    font-size: 20rpx;
    /* width: 254rpx; */
    height: 30rpx;
    color: #a6a6a6;
    line-height: 30rpx;
}

.good_content .good_footer {
    box-sizing: border-box;
    float: left;
    /* margin-top: 16rpx; */
    margin-left: 20rpx;
    width: 450rpx;
    height: 90rpx;
}

.good_content .content .price {
    font-size: 36rpx;
    color: #e4452a;
    margin-top: 10rpx;
}

.good_content .good .stepper {
    float: left;
    margin-top: 15rpx;
    margin-left: 60rpx;
    width: 162rpx;
    height: 50rpx;
}

.good_content .good_footer .price {
    float: left;
    width: 38%;
    height: 50rpx;
    /* margin-top: 15rpx; */
    color: #a6a6a6;
    font-size: 16rpx;
    margin-top: 10rpx;
}

.good_content .good_footer .price2 {
    font-size: 28rpx;
    color: #f00;
    font-weight: bold;
}

.good_content .good_footer .stepper {
    float: left;
    /* margin-top: 10rpx; */
    width: 36%;
    margin-left: 110rpx;
}

.good_content .stepper .add,
.stepper .subtract {
    float: left;
    width: 50rpx;
    height: 50rpx;
    line-height: 50rpx;
    text-align: center;
    font-size: 28rpx;
    border: 1px solid #ccc;
    box-sizing: border-box;
}

.good_content .stepper .disabled {
    float: left;
    color: #eee;
}

.good_content .stepper .count {
    float: left;
    width: 60rpx;
    height: 50rpx;
    line-height: 50rpx;
    border: 0px solid #ccc;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    text-align: center;
    box-sizing: border-box;
    /* min-height: 1rem; */
    font-size: 28rpx;
    min-height: 50rpx;
}

/*******底部********************************************  */

.section-bottom {
    display: flex;
    flex-direction: row;
    align-items: center;
    width: 100%;
    height: 100rpx;
    position: fixed;
    bottom: 0;
    left: 0;
    margin-bottom: 0;
    background-color: #fff;
    box-sizing: border-box;
    z-index: 99;
}

.section-bottom .btn {
    width: 160rpx;
    height: 100rpx;
    line-height: 100rpx;
    text-align: center;
    color: #fdfffd;
    background-color: #fbb304;
}

.section-bottom .btn-default {
    color: #fff;
    background-color: #ff703a;
}

.section-bottom .btn-primary {
    color: #fdfffd;
    background-color: #e83632;
}

.section-bottom .checkbox-allcheck {
    display: inline-block;
    margin-left: 30rpx;
    vertical-align: top;
    font-size: 0px;
}

.section-bottom .check-all-text {
    display: inline-block;
    vertical-align: top;
    margin-left: 10rpx;
    height: 23px;
    line-height: 23px;
    font-size: 28rpx;
}

.section-bottom .total {
    flex: 1;
    text-align: center;
    font-size: 26rpx;
}

.total .totalCount {
    font-size: 26rpx;
    display: inline-block;
    vertical-align: middle;
}

.total .totalPrice {
    display: inline-block;
    font-size: 26rpx;
    margin-left: 20rpx;
    vertical-align: middle;
    font-weight: bold;
}

.total .totalCount span {
    color: #e4452a;
    vertical-align: middle;
}

.total .totalPrice span {
    color: #e4452a;
    font-size: 36rpx;
    vertical-align: middle;
}

/*   空购物车  */

.cartList {
    padding: 100px 0 0 0;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    color: #999;
}

.cartList image {
    width: 66px;
    height: 66px;
    margin-bottom: 20px;
}
</style>
